home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3 / CHAPTER8 / CRTDTSRC.C < prev    next >
C/C++ Source or Header  |  1996-04-28  |  7KB  |  217 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "crtdtsrc.h"
  5. #include "sqlext.h"  
  6. #include "odbcinst.h"  
  7.  
  8.  
  9. #if defined (WIN32)
  10.     #define IS_WIN32 TRUE
  11. #else
  12.     #define IS_WIN32 FALSE
  13. #endif
  14.  
  15. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  16. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  17. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  18.  
  19. HINSTANCE hInst;   // current instance
  20.  
  21. LPCTSTR lpszAppName = "MyApp";
  22. LPCTSTR lpszTitle   = "SQLCreateDataSource()"; 
  23.  
  24.  
  25. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  26.  
  27.  
  28. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  29.                       LPTSTR lpCmdLine, int nCmdShow)
  30. {
  31.    MSG      msg;
  32.    HWND     hWnd; 
  33.    WNDCLASS wc;
  34.  
  35.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  36.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  37.    wc.cbClsExtra    = 0;                      
  38.    wc.cbWndExtra    = 0;                      
  39.    wc.hInstance     = hInstance;              
  40.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  41.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  42.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  43.    wc.lpszMenuName  = lpszAppName;              
  44.    wc.lpszClassName = lpszAppName;              
  45.  
  46.    if ( IS_WIN95 )
  47.    {
  48.       if ( !RegisterWin95( &wc ) )
  49.          return( FALSE );
  50.    }
  51.    else if ( !RegisterClass( &wc ) )
  52.       return( FALSE );
  53.  
  54.    hInst = hInstance; 
  55.  
  56.    hWnd = CreateWindow( lpszAppName, 
  57.                         lpszTitle,    
  58.                         WS_OVERLAPPEDWINDOW, 
  59.                         CW_USEDEFAULT, 0, 
  60.                         CW_USEDEFAULT, 0,  
  61.                         NULL,              
  62.                         NULL,              
  63.                         hInstance,         
  64.                         NULL               
  65.                       );
  66.  
  67.    if ( !hWnd ) 
  68.       return( FALSE );
  69.  
  70.    ShowWindow( hWnd, nCmdShow ); 
  71.    UpdateWindow( hWnd );         
  72.  
  73.    while( GetMessage( &msg, NULL, 0, 0) )   
  74.    {
  75.       TranslateMessage( &msg ); 
  76.       DispatchMessage( &msg );  
  77.    }
  78.  
  79.    return( msg.wParam ); 
  80. }
  81.  
  82.  
  83. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  84. {
  85.    WNDCLASSEX wcex; 
  86.  
  87.    wcex.style         = lpwc->style;
  88.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  89.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  90.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  91.    wcex.hInstance     = lpwc->hInstance;
  92.    wcex.hIcon         = lpwc->hIcon;
  93.    wcex.hCursor       = lpwc->hCursor;
  94.    wcex.hbrBackground = lpwc->hbrBackground;
  95.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  96.    wcex.lpszClassName = lpwc->lpszClassName;
  97.  
  98.    // Added elements for Windows 95.
  99.    //...............................
  100.    wcex.cbSize = sizeof(WNDCLASSEX);
  101.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  102.                             IMAGE_ICON, 16, 16,
  103.                             LR_DEFAULTCOLOR );
  104.             
  105.    return RegisterClassEx( &wcex );  
  106. }
  107.  
  108.  
  109. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  110. {
  111. static HWND  hList  = NULL;
  112. static UCHAR szDS[] = "New Data Source";
  113.  
  114.    switch( uMsg )
  115.    {
  116.       case WM_CREATE :
  117.               {
  118.                  hList = CreateWindow( "LISTBOX", "",    
  119.                                        LBS_NOTIFY | WS_VSCROLL | 
  120.                                        WS_BORDER  | WS_CHILD | 
  121.                                        WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  122.                                        0, 0, 
  123.                                        0, 0,  
  124.                                        hWnd,              
  125.                                        (HMENU)101,              
  126.                                        hInst,         
  127.                                        NULL               
  128.                                      );
  129.               }
  130.               break;
  131.  
  132.       case WM_SIZE :
  133.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  134.                                        HIWORD( lParam ), TRUE );
  135.               break; 
  136.               
  137.       case WM_COMMAND :
  138.               switch( LOWORD( wParam ) )
  139.               {
  140.                  case IDM_TEST :
  141.                         {
  142.                            BOOL  bRet;
  143.                            UCHAR szLBStr[128];
  144.  
  145.                            // Call SQLCreateDataSource() to 
  146.                            // create a new data source named
  147.                            // "New Data Source".
  148.                            // ..............................
  149.                            bRet = SQLCreateDataSource( hWnd, szDS );
  150.  
  151.                            strcpy( szLBStr, "SQLCreateDataSource() " );
  152.  
  153.                            // Display status to the user.
  154.                            // ...........................
  155.                            if( bRet )
  156.                            {
  157.                               strcat( szLBStr, "successful" );
  158.  
  159.                               SendMessage( hList, LB_ADDSTRING, 0, 
  160.                                           (LPARAM)szLBStr );
  161.                            }
  162.                            else
  163.                            {
  164.                               strcat( szLBStr, "not successful" );
  165.  
  166.                               SendMessage( hList, LB_ADDSTRING, 0, 
  167.                                           (LPARAM)szLBStr );
  168.                            }
  169.                         }
  170.                         break;
  171.  
  172.                  case IDM_ABOUT :
  173.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  174.                         break;
  175.  
  176.                  case IDM_EXIT :
  177.                         DestroyWindow( hWnd );
  178.                         break;
  179.               }
  180.               break;
  181.       
  182.       case WM_DESTROY :
  183.               PostQuitMessage(0);
  184.               break;
  185.  
  186.       default :
  187.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  188.    }
  189.  
  190.    return( 0L );
  191. }
  192.  
  193.  
  194.  
  195. LRESULT CALLBACK About( HWND hDlg,           
  196.                         UINT message,        
  197.                         WPARAM wParam,       
  198.                         LPARAM lParam)
  199. {
  200.    switch (message) 
  201.    {
  202.        case WM_INITDIALOG: 
  203.                return (TRUE);
  204.  
  205.        case WM_COMMAND:                              
  206.                if (   LOWORD(wParam) == IDOK         
  207.                    || LOWORD(wParam) == IDCANCEL)    
  208.                {
  209.                        EndDialog(hDlg, TRUE);        
  210.                        return (TRUE);
  211.                }
  212.                break;
  213.    }
  214.  
  215.    return (FALSE); 
  216. }
  217.